package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Businesstrip;
import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.service.BusinesstripService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BusinesstripController {
    @Autowired
    BusinesstripService busService;
    @Autowired
    HttpSession session;

    // boss用户查询所有竞争 否则查询当前部门
    @RequestMapping("/business-list")
    @ResponseBody
    public Map<String,Object> allBusinessList(Integer page, Integer size,Integer filter,Integer vague,String content) {
        Page<Object> objects = PageHelper.startPage(page, size);
        Employee emp = (Employee) session.getAttribute("emp");
        List<Businesstrip> bus = busService.selectBusiness(emp.getDepartment().getDeptId(),filter,vague,content);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", bus);
        map.put("total", objects.getTotal());
        return map;
    }
    // 按出差Id查询出差
    @RequestMapping("/business-id")
    @ResponseBody
    public Businesstrip businessById(Integer businessId){
        Businesstrip bus = busService.selectBusinessById(businessId);
        return bus;
    }
    // 按机会Id查询员工报价集合
    @RequestMapping("/business-opp-list")
    @ResponseBody
    public List<Businesstrip> allBusinessOppIdList(Integer oppId){
        List<Businesstrip> bus = busService.selectBusinessByOppId(oppId);
        return bus;
    }

    @PostMapping("/save-business")
    @ResponseBody
    public void saveBusiness(@RequestBody Businesstrip business){
        busService.saveBusiness(business);
    }

    @PostMapping("/delete-business")
    @ResponseBody
    public void deleteBusiness(@RequestParam(value = "businessId") Integer businessId){
        busService.deleteBusiness(businessId);
    }
}
